package portal.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import portal.facade.PortalComprasFacadeStateful;
import portal.vo.CompraVO;
import portal.vo.UsuarioVO;
import portal.web.businessDelegate.PortalComprasDelegate;

@SuppressWarnings("serial")
public class CerrarCompraServlet extends HttpServlet {

	private static Logger logger = Logger.getLogger(CerrarCompraServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		processRequest(req, resp);
	}

	private void processRequest(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>CerrarCompraServlet<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		HttpSession session = req.getSession(true);
		PortalComprasFacadeStateful facadeStateful = (PortalComprasFacadeStateful) session.getAttribute("facadeStateful");
		facadeStateful.confirmarCompra();
		
		List<CompraVO> listado = new ArrayList<CompraVO>();
		
		UsuarioVO usuarioVO = (UsuarioVO)req.getSession().getAttribute("usuarioInfo");
	
		listado = PortalComprasDelegate.getInstance().getListaComprasByUsuario(usuarioVO);
		
		req.getSession().setAttribute("listaCompras", listado);
		
		System.out.println("Compras:" + listado.size() );	
		

		//req.getRequestDispatcher("/jsp/comprasRealizadas.jsp").forward(req, resp);
		req.getRequestDispatcher("/jsp/paginaPrincipal.jsp").forward(req, resp);
		

	}
}
